package com.n9.buymsg;

import com.n9.buymsg.event.AppEvent;
import com.n9.buymsg.event.Callback;
import com.n9.buymsg.event.EventProcessor;
import com.n9.buymsg.event.EventPublisher;
import com.n9.buymsg.service.qqonline.converter.MsgBodyFilter;
import com.n9.buymsg.service.qqonline.converter.MsgTypeFilter;
import com.n9.buymsg.view.vo.QqVo;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 程序上下文
 * @Author: ice
 * @Date: 2020-06-24 10:18
 */
public class AppContext implements EventPublisher {
    private String account;//账号
    private String password;//密码

    private MsgTypeFilter msgTypeFilter;
    private MsgBodyFilter msgBodyFilter;

    private Map<Long, QqVo> qqVoMap = new ConcurrentHashMap<>();

    private static final AppContext CONTEXT = new AppContext();//单例上下文

    private AppContext(){};

    public static final AppContext getInstance() {
        return CONTEXT;
    }

    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }

    public Collection<QqVo> getQqVos() {
        return qqVoMap.values();
    }


    public void initMsgTypeFilter(String[] msgTypes) {
        this.msgTypeFilter = new MsgTypeFilter(msgTypes);
    }

    public void initMsgBodyFilter(String[] keywords) {
        this.msgBodyFilter = new MsgBodyFilter(keywords);
    }

    public MsgTypeFilter getMsgTypeFilter() {
        return msgTypeFilter;
    }
    public MsgBodyFilter getMsgBodyFilter() {
        return msgBodyFilter;
    }


    private EventProcessor eventInvoker = new EventProcessor();
    @Override
    public void publishEvent(AppEvent appEvent) {
        eventInvoker.process(appEvent);
    }

    @Override
    public void publishEvent(AppEvent appEvent, Callback callback) {
        eventInvoker.process(appEvent, callback);
    }
}
